unit LayerMerge;

interface

uses
  Windows, Classes, Graphics,
  Jpeg, ieview, imageenview, ExtCtrls, ComCtrls, StdCtrls, ExtDlgs,
  imageenproc, imageenio, HYIEDefs, HYieutils, ieopensavedlg; // ievect,


Type
  TLayerMerge = Class(TComponent)
    private
      FImg: TImageEnView;
      procedure Do_Merge(src:TImageEnView);
    public
      procedure Merge(src:TImageEnView; var aBitMap:TBitMap);
      procedure SaveTif(src:TImageEnView; fileName:String);
      Constructor Create(aOwner:TComponent); override;
      Destructor  Destroy; override;
  End;  


implementation

{ TLayerMerge }

constructor TLayerMerge.Create(aOwner: TComponent);
begin
  inherited;
  FImg := TImageEnView.Create(self);
  FImg.Visible := False;
  FImg.IO.AsyncMode := False;
end;

destructor TLayerMerge.Destroy;
begin

  inherited;
end;

procedure TLayerMerge.Do_Merge(src: TImageEnView);
var
  iLoop: Integer;
begin
  FImg.Clear;
  FImg.LayersClear;

  for iLoop := 0 to src.LayersCount - 1 do
  begin
    if iLoop > 0 then FImg.LayersAdd;
    FImg.Layers[iLoop].Assign( src.Layers[iLoop] );
  end;
  FImg.LayersMergeAll;
end;

procedure TLayerMerge.Merge(src: TImageEnView; var aBitMap: TBitMap);
begin
  if Assigned(aBitMap) then aBitMap.free;

  Do_Merge(Src);
  
  aBitMap := TBitMap.Create;
  aBitMap.Assign( FImg.Bitmap );
end;

procedure TLayerMerge.SaveTif(src: TImageEnView; fileName:String);
begin
  Do_Merge(Src);
  
  with FImg do
  begin
    IO.Params.DpiX := 204;
    IO.Params.DpiY := 196;
    IO.Params.TIFF_Compression := ioTIFF_G4FAX;        // ;ioTIFF_G3FAX2D;
    IO.Params.BitsPerSample := 1;
    IO.Params.SamplesPerPixel := 1;
    IO.SaveToFile(filename);
  end;

end;

end.
